<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="40%">
    <el-form ref="formItem" :model="formItem" label-width="80px" size="small" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户编号" prop="userId">
            <el-input v-model="formItem.userId" :disabled="isEdit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="formItem.password"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="formItem.name"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="入职日期" prop="startDate">
            <el-date-picker v-model="formItem.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="离职日期" prop="endDate">
            <el-date-picker v-model="formItem.endDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="权限" prop="limit">
            <el-input v-model="formItem.limit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="depart">
            <el-input v-model="formItem.depart"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="岗位" prop="job">
            <el-input v-model="formItem.job"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="phone">
            <el-input v-model="formItem.phone"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="生日" prop="birth">
            <el-input v-model="formItem.birth"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="formItem.sex" size="small">
              <el-radio label="1" border>男</el-radio>
              <el-radio label="2" border>女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { modalFormMixin } from '@/utils/mixin'
import { addUser, updateUser } from '@/api/user'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      formItem: {
        id: null,
        userId: '',
        password: '',
        name: '',
        startDate: '',
        endDate: '',
        limit: '',
        depart: '',
        job: '',
        phone: '',
        birth: '',
        sex: ''
      },
      rules: {
        userId: [{ required: true, message: '请输入id', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
      }
    }
  },
  mixins: [modalFormMixin],
  methods: {
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updateUser(this.formItem) : await addUser(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>